#include "stdafx.h"
#include "wf_infoangle.h"

#include "pf_information.h"
#include "pf_snapper.h"



WF_InfoAngle::WF_InfoAngle(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Info Angle",
                           container, graphicView) {}


QAction* WF_InfoAngle::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
/*      QAction* action = new QAction(tr("Angle between two lines"),
                                  tr("&Angle between two lines"),
                                  QKeySequence(), NULL); */

    QAction* action = new QAction(tr("Angle between two lines"), NULL);
    //action->zetStatusTip(tr("Measures the angle between two lines"));
	action->setIcon(QIcon(":/extui/infoangle.png"));
    return action;
}


void WF_InfoAngle::init(int status) {
    PF_ActionInterface::init(status);

}



void WF_InfoAngle::trigger() {

    PF_DEBUG->print("PF_ActionInfoAngle::trigger()");

    if (entity1!=NULL && entity2!=NULL) {
        PF_VectorSolutions sol =
            PF_Information::getIntersection(entity1, entity2, false);

        if (sol.hasValid()) {
            intersection = sol.get(0);

            if (intersection.valid && point1.valid && point2.valid) {
                double angle1 = intersection.angleTo(point1);
                double angle2 = intersection.angleTo(point2);
                double angle = fabs(angle2-angle1);

                QString str;
                str.sprintf("%.6f", PF_Math::rad2deg(angle));
                PF_DIALOGFACTORY->commandMessage(tr("Angle: %1%2")
                                                 .arg(str).arg(QChar(0xB0)));
            }
        } else {
            PF_DIALOGFACTORY->commandMessage(tr("Lines are parallel"));
        }
    }
}



void WF_InfoAngle::mouseMoveEvent(PF_MouseEvent* /*e*/) {
    PF_DEBUG->print("PF_ActionInfoAngle::mouseMoveEvent begin");

    switch (getStatus()) {
    case SetEntity1:
        break;

    case SetEntity2:
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionInfoAngle::mouseMoveEvent end");
}



void WF_InfoAngle::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {

        PF_Vector mouse(graphicView->toGraphX(e->x()),
                        graphicView->toGraphY(e->y()));

        switch (getStatus()) {
        case SetEntity1:
            entity1 = catchEntity(e);
            if (entity1!=NULL && entity1->rtti()==LX_Define::EntityLine) {
                point1 = entity1->getNearestPointOnEntity(mouse);
                setStatus(SetEntity2);
            }
            break;

        case SetEntity2:
            entity2 = catchEntity(e);
            if (entity2!=NULL && entity2->rtti()==LX_Define::EntityLine) {
                point2 = entity2->getNearestPointOnEntity(mouse);
                trigger();
                setStatus(SetEntity1);
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_InfoAngle::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetEntity1:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify first line"),
            tr("Cancel"));
        break;
    case SetEntity2:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify second line"),
            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_InfoAngle::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_InfoAngle::updateToolBar() {
    switch (getStatus()) {
    case SetEntity1:
    case SetEntity2:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    default:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarInfo);
        break;
    }
}


// EOF
